﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.Design.Behavior;
using System.Drawing;
using System.Windows.Forms;

namespace GradienLabelComponent
{
    /*
     * Класс маркера
     */
    class ZoomGlyph : Glyph
    {
        Control control;
        BehaviorService behaviorService;
        Point center;

        public ZoomGlyph(BehaviorService behaviorService, Control control)
            : base(new ZoomBehavior(behaviorService, control))
        {
            this.control = control;
            this.behaviorService = behaviorService;
        }

        // Границы маркера
        public override Rectangle Bounds
        {
            get
            {                                
                Size size = control.Size;
                Point edge = behaviorService.ControlToAdornerWindow(control);
                center = new Point(edge.X + (size.Width / 2), edge.Y + (size.Height / 2));
                Rectangle bounds = new Rectangle(center.X - 5, center.Y - 5, 10, 10);
                return bounds;
            }
        }
        public override Cursor GetHitTest(Point p)
        {
            // Если курсор находится в заданной точке, вызываем курсор Hand
            if (Bounds.Contains(p))
            {
                return Cursors.Hand;
            }
            return null;
        }

        // Рисуем окружность с крестиком
        public override void Paint(PaintEventArgs pe)
        {
            pe.Graphics.FillEllipse(Brushes.Blue, Bounds);
            Pen pen = new Pen(Color.Red);
            pe.Graphics.DrawLine(pen, center.X - 4, center.Y, center.X + 4, center.Y);
            pe.Graphics.DrawLine(pen, center.X, center.Y - 4, center.X, center.Y - 4);
        }
    }

    /*
     * Класс поведения маркера
     */
    class ZoomBehavior : Behavior
    {

        BehaviorService behaviorService;
        Control control;

        public ZoomBehavior(BehaviorService behaviorService, Control control)
        {
            this.behaviorService = behaviorService;
            this.control = control;
        }

        public override bool OnMouseUp(Glyph g, MouseButtons button)
        {
            if (button == MouseButtons.Left)
            {
                if (control.Width < control.Parent.Width - 10 && control.Height < control.Parent.Height - 10)
                {
                    control.Left -= 5;
                    control.Top -= 5;
                    control.Width += 10;
                    control.Height += 10;
                    behaviorService.SyncSelection();
                }
            }
            if (button == MouseButtons.Right)
            {
                if (control.Width > 10 && control.Height > 10)
                {
                    control.Left += 5;
                    control.Top += 5;
                    control.Width -= 10;
                    control.Height -= 10;
                    behaviorService.SyncSelection();
                }
            }
            return true;
        }
    }
}
